Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
A way to "cache" a value in Puppeteer's waitForFunction?

Is there a way to "cache" a value in Puppeteer's browser-based functions?

The callback passed to page.waitForFunction( .. ) is running on every DOMMutation from the puppeteer option polling: "mutation". So it could potentially run 500 times and execute const listEl = document.querySelector("#list") 500 times.

I was wondering if there is a way to cache listEl when it's found so that next time the callback is invoked querySelector("#listEl") does not have to run again and the cached value inside listEl is used.

await page.waitForFunction(
    () => {
      const listEl = document.querySelector("#list");
    },
    {
      polling: "mutation",
    }
  );
})();

The callback inside waitForFunction( ..) has no access to variables outside the function. Because the callback runs (injected) inside Browser context and the outside environment is node.js.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Well you could use localStorage. As puppeteer is using chromium, localStorage is therefore supported.

await page.evaluate(() => {
  localStorage.setItem('key', 'value');
});

You could even probably pass the localStorage value through different executions on a same domain via userDataDir launch option.

const browser = await puppeteer.launch({
  userDataDir: './temp/',
  // ...
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could just make it a window property:

window.listEl = window.listEl || document.querySelector("#list")
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda